<?php
require_once('global.php');
//搜索
if ($_POST['action'] == 'search') {
	$searchfrom = in_array($_POST['searchfrom'], array('feed', 'comment')) ? $_POST['searchfrom'] : 'feed';
	//if ($options['rewrite_enable']) {
	//	$searchurl = 'search.'.$options['rewrite_ext'];
	//} else {
		$searchurl = './';
	//}
	if ((!$options['allow_search_comments']) && $searchfrom == 'comment') {
		message('系统不允许在评论里执行搜索操作', $searchurl);
	}
	$keywords = trim($_POST['keywords'] ? $_POST['keywords'] : $_GET['keywords']);
		if (!$keywords) {
			message('您没有指定要搜索的关键字.', $searchurl);
		} else {
			if(strlen($keywords) < $options['search_keywords_min_len']) {
				message('关键字不能少于'.$options['search_keywords_min_len'].'个字节.', $searchurl);
			}
		}
		$searchindex = array('id' => 0, 'addtime' => '0');
		$query = $db->query("SELECT searchid, addtime, ('".$options['search_post_space']."'<>'0' AND $timestamp-addtime<".$options['search_post_space'].") AS flood, searchfrom='$searchfrom' AND keywords='$keywords' AS indexvalid FROM {$tablepre}xna_search ORDER BY searchid");
		while($index = $db->row($query)) {
			if($index['indexvalid'] && $index['addtime'] > $searchindex['addtime']) {
				$searchindex = array('id' => $index['searchid'], 'addtime' => $index['addtime']);
				break;
			} elseif($index['flood']) {
				message('对不起,您在 '.$options['search_post_space'].' 秒内只能进行一次搜索.', $searchurl);
			}
		}
		if($searchindex['id']) {
			$searchid = $searchindex['id'];
			$gourl = 'search.php?searchid='.$searchid;
		} else {
			$keywords = str_replace("_","\_",$keywords);
			$keywords = str_replace("%","\%",$keywords);
			if(preg_match("(AND|\+|&|\s)", $keywords) && !preg_match("(OR|\|)", $keywords)) {
				$andor = ' AND ';
				$sqltxtsrch = '1';
				$keywords = preg_replace("/( AND |&| )/is", "+", $keywords);
			} else {
				$andor = ' OR ';
				$sqltxtsrch = '0';
				$keywords = preg_replace("/( OR |\|)/is", "+", $keywords);
			}
			$keywords = str_replace('*', '%', addcslashes($keywords, '%_'));
			
			foreach(explode("+", $keywords) AS $text) {
				$text = trim($text);
				if($text) {
					$sqltxtsrch .= $andor;
					if ($searchfrom == "feed") {
						$sqltxtsrch .= ($_POST['searchin'] == 'content') ? "(content LIKE '%".str_replace('_', '\_', $text)."%' OR description LIKE '%".$text."%' OR feed_title LIKE '%".$text."%')" : "feed_title LIKE '%".$text."%'";
					} else {
						$sqltxtsrch .= ($_POST['csearchin'] == 'author') ? "author LIKE '%".$text."%'" : "(content LIKE '%".str_replace('_', '\_', $text)."%' OR author LIKE '%".$text."%')";
					}
					//echo $sqltxtsrch;
				}
			}
			//搜索文章
			//echo $searchfrom;
			if ($searchfrom == 'feed') {
				$sSql = "SELECT feedid FROM {$tablepre}xna_feed WHERE state!='0'";
				$sortby = in_array($_POST['sortby'], array('addtime', 'views', 'comments', 'cid')) ? $_POST['sortby'] : 'addtime';
				$orderby = $_POST['orderby'] == 'asc' ? 'asc' : 'desc';
				$cid = $_POST['cid'];
				
				//分类
				$catearray = array();
				if($cid) {
					foreach((is_array($cid) ? $cid : explode('_', $cid)) as $cateid) {
						if($cateid = intval(trim($cateid))) {
							$catearray[] = $cateid;
						}
					}
				}
				$cids = $comma = '';
				foreach($catecache as $data) {
					if(!$catearray || in_array($data['cid'], $catearray)) {
						$cids .= $comma.intval($data['cid']);
						$comma = ',';
					}
				}
				if ($cids) {
					$sSql .= " AND cid IN ($cids)";
				}
				$sSql .= " AND ($sqltxtsrch) ORDER BY $sortby $orderby";
				$tatols = $ids = 0;
				$query = $db->query($sSql);
				while($feed = $db->row($query)) {
					$ids .= ','.$feed['feedid'];
					$tatols++;
				}
				$searchfrom = 'article';
				$gourl = 'search.php?searchid=';				
			//搜索评论
			} else {
				$sSql  = "SELECT commid FROM {$tablepre}xna_comment WHERE state='1'";
				$sSql .= " AND ($sqltxtsrch) ORDER BY addtime DESC";
				$tatols = $ids = '0';
				$query = $db->query($sSql);
				while($article = $db->row($query)) {
					$ids .= ','.$article['commentid'];
					$tatols++;
				}
				$searchfrom = 'comment';
				$gourl = 'search.php?action=comments&amp;searchid=';
			}
			$db->row($query);								
			$db->query("INSERT INTO {$tablepre}xna_search (keywords, addtime, sortby, orderby, tatols, ids, searchfrom, ipaddress) VALUES ('".char_cv($keywords)."', '$timestamp', '$sortby', '$orderby', '$tatols', '$ids', '$searchfrom', '$onlineip')");
			$searchid = $db->insert_id();
			$gourl = 'search.php?searchid='.$searchid;
		}
		if ($options['showmsg']) {
			$gourl = str_replace("&amp;", "&", $gourl);
			message('搜索成功完成,现在将转入结果页面.', $gourl);
		} else {
			$gourl = str_replace("&amp;", "&", $gourl);
			@header("Location: ".$gourl);
		}
}
	$searchid = intval($_GET['searchid']);
	if (!$searchid){
		$nolist = TRUE;
	} else {
		$search = $db->fetch_one_array("SELECT * FROM {$tablepre}xna_search WHERE searchid='".$searchid."'");
		/*if (!$search) {
			message('您指定的搜索不存在或已过期,请返回.', './');
		} elseif ($search['searchfrom'] != 'feed') {
			message('您指定的搜索不存在或已过期,请返回.', './');
		}*/
		$tatol = $search['tatols'];
		$pagenum = '20';
		$pageurl = 'search.php?searchid='.$searchid;
		if($page) {
			$start_limit = ($page - 1) * $pagenum;
		} else {
			$start_limit = 0;
			$page = 1;
		}
		
		$sSql = "SELECT 
			a.feedid,a.site_name,a.siteid,a.state,a.addtime,a.feed_title,a.feed_url,feed_description
		FROM {$tablepre}xna_feed a 
			WHERE a.state!='0'";
		$sSql .= " AND a.feedid IN (".$search['ids'].") ORDER BY a.".$search['sortby']." ".$search['orderby']." LIMIT $start_limit, ".$pagenum;
		$query = $db->query($sSql);		
		$multipage = multi($tatol, $pagenum, $page, $pageurl);	
		$navtext = ''.$search['keywords'];
		$feeddb = array();
		while ($feed = $db -> row($query)) {
			$feed['addtime'] = date("Y-m-d", $feed['addtime']);
			$feeddb[] = $feed;
		}
	}
include template("search", _ROOT ."/template/digg/");
?>